package com.lotto.manager.admin.service.impl;


import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lotto.manager.admin.dao.SysConfigMapper;
import com.lotto.manager.admin.entity.db.SysConfig;
import com.lotto.manager.admin.service.SysConfigService;
import com.lotto.manager.admin.system.SystemData;
import com.lotto.manager.common.constant.SystemConstant;


@Service
public class SysConfigServiceImpl implements SysConfigService {
	
	@Autowired
	public SysConfigMapper sysConfigMapper;
	
	@Override
	public void init() {
		initNumber();
	}
	
	
	@Override
	public String getNatureByNumber(int type,String number) {
		switch (type) {
		case 1:
			return SystemData.zodiacNumber.get(number);
		case 2:
			return SystemData.wuXingNumber.get(number);
		case 3:
			return SystemData.colorNumber.get(number);
		case 4:
			return SystemData.sunSingleDoubleNumber.get(number);
		case 5:
			return SystemData.singleDoubleNumber.get(number);
		case 6:
			return SystemData.bigSmallNumber.get(number);
		}
		return null;
	}
	

	public void initNumber() {
		initSysConfig();
		initZodiacs();
		initWuXing();
		initColor();
		initSunSingleDouble();
		initSingleDouble();
		initBigSmall();
	}
	
	public void initSysConfig(){
		SysConfig config= sysConfigMapper.selectByPrimaryKey(1);
		SystemData.addSysConfig(config);
	}


	public   void initZodiacs() {
		String thisYear = SystemData.getSysConfig().getZodiac();
		int numberIndex = 0;
		for (int i = 0; i < SystemConstant.ZODIACS.length; i++) {
			if (thisYear.equals(SystemConstant.ZODIACS[i])) {
				numberIndex = i;
				break;
			}
		}
		addMapData(SystemData.zodiacNumber, SystemConstant.ZODIACS, numberIndex);
	}

	public   void initWuXing() {
		addMapData(SystemData.wuXingNumber, SystemConstant.WUXING, 0);
	}

	public   void initColor() {
		int basis = 0;
		int index = 0;
		for (int i = 0; i < SystemConstant.BASISNUMBER.length; i++) {
			SystemData.colorNumber.put(SystemConstant.BASISNUMBER[i],
					SystemConstant.COLOR[index]);
			/*System.out.println(SystemConstant.BASISNUMBER[i] + "|"
					+ SystemConstant.WAVECOLOR[index]);*/
			basis++;
			if (basis >= 2 || 0 == (i + 1) % 10) {
				if (basis >= 2 && 0 == (i + 1) % 10) {
					basis = 1;
				} else {
					basis = 0;
				}
				index++;
			}
			if (index >= (SystemConstant.COLOR.length)) {
				index = 0;
			}
		}
	}
	
	
	public   void initSunSingleDouble() {
		for (int i = 0; i < SystemConstant.BASISNUMBER.length; i++) {
			int bits = Integer.valueOf(SystemConstant.BASISNUMBER[i].substring(0, 1));
			int ten = Integer.valueOf(SystemConstant.BASISNUMBER[i].substring(1, 2));
			if((bits+ten)%2 == 0){
				SystemData.sunSingleDoubleNumber.put(SystemConstant.BASISNUMBER[i], "合双");
				/*System.out.println(SystemConstant.BASISNUMBER[i] + "|"
						+ "合双");*/
			}else{
				SystemData.sunSingleDoubleNumber.put(SystemConstant.BASISNUMBER[i], "合单");
				/*System.out.println(SystemConstant.BASISNUMBER[i] + "|"
						+ "合单");*/
			}
		}
	}
	
	public   void initSingleDouble() {
		for (int i = 0; i < SystemConstant.BASISNUMBER.length; i++) {
			if("49".equals(SystemConstant.BASISNUMBER[i])){
				SystemData.singleDoubleNumber.put(SystemConstant.BASISNUMBER[i], "和");
				/*System.out.println(SystemConstant.BASISNUMBER[i] + "|"
						+ "和");*/
			}else if(Integer.valueOf(SystemConstant.BASISNUMBER[i])%2 == 0){
				SystemData.singleDoubleNumber.put(SystemConstant.BASISNUMBER[i], "双");
				/*System.out.println(SystemConstant.BASISNUMBER[i] + "|"
						+ "双");*/
			}else{
				SystemData.singleDoubleNumber.put(SystemConstant.BASISNUMBER[i], "单");
				/*System.out.println(SystemConstant.BASISNUMBER[i] + "|"
						+ "单");*/
			}
		}
	}
	
	public   void initBigSmall(){
		for (int i = 0; i < SystemConstant.BASISNUMBER.length; i++) {
			if("49".equals(SystemConstant.BASISNUMBER[i])){
				SystemData.bigSmallNumber.put(SystemConstant.BASISNUMBER[i], "和");
			}else if(Integer.valueOf(SystemConstant.BASISNUMBER[i])>=25){
				SystemData.bigSmallNumber.put(SystemConstant.BASISNUMBER[i], "大");
				/*System.out.println(SystemConstant.BASISNUMBER[i] + "|"
						+ "双");*/
			}else{
				SystemData.bigSmallNumber.put(SystemConstant.BASISNUMBER[i], "小");
				/*System.out.println(SystemConstant.BASISNUMBER[i] + "|"
						+ "单");*/
			}
		}
	}
	
	
	public   void addMapData(Map<String, String> map, String[] basisData,
			int index) {
		for (int i = 0; i < SystemConstant.BASISNUMBER.length; i++) {
			map.put(SystemConstant.BASISNUMBER[i], basisData[index]);
			index++;
			if (index >= (basisData.length)) {
				index = 0;
			}
		}
	}


	

}